﻿--[[
   @brief    branch_selector
   @author   lieberli
   @version  1.0
   @date     2019-05-22
--]]

branch_selector_control = {
    wait_branch_info_games = {};

    init = function()
        base_utility.log("[branch_selector_control]init");
    end;
    
    open_branch_selector = function(event_id, data)
        base_utility.log("[branch_selector_control]open_branch_selector");
        local action_info = base_utility.json_to_table(data);
        if action_info == nil then
            return;
        end;
        
        local param = {};
        param["game_id"] = action_info.game_id;
        local data_out = comm_center.call_service("Svr_CheckBranchInfoReady", param);
        if data_out["is_ready"] or (not data_out["need_show_branch_selector"]) then
            ierd_tgp.tpf_ui.Require("branch_selector\\view.lua");        
            branch_selector_view.open_branch_selector(action_info);
        else
            table.insert(branch_selector_control.wait_branch_info_games, action_info.game_id);
            local tpfui = ierd_tgp.tpf_ui.GetTPFUI();
            if nil ~= tpfui then
                tpfui:AddInterval("branch_selector_control.on_wait_branch_info_timeout", 3000, tonumber(action_info.game_id));
            end;
        end
    end;
    
    on_update_branch_info = function(event_id, data)
        if base_utility.is_empty_table(branch_selector_control.wait_branch_info_games) then
            return;
        end
        
        local param = base_utility.json_to_table(data);
        base_utility.log("[branch_selector_control]on_update_branch_info, " .. param["game_id"]);
        
        local pos = nil;
        for key, value in pairs(branch_selector_control.wait_branch_info_games) do
            if tostring(value) == tostring(param["game_id"]) then
                pos = key;
            end
        end

        if pos ~= nil then
            base_utility.log("[branch_selector_control]on_update_branch_info, open_branch_selector");
            ierd_tgp.tpf_ui.Require("branch_selector\\view.lua");        
            branch_selector_view.open_branch_selector({game_id = param["game_id"]});
            table.remove(branch_selector_control.wait_branch_info_games, pos);
            local tpfui = ierd_tgp.tpf_ui.GetTPFUI();
            if nil ~= tpfui then
                tpfui:RemoveInterval("branch_selector_control.on_wait_branch_info_timeout", tonumber(param["game_id"]));
            end;
        end
    end;
    
    on_wait_branch_info_timeout = function(UserData, TimeStamp)
        base_utility.log("[branch_selector_control]on_wait_branch_info_timeout, userdata=" .. UserData);
        local pos = nil;
        for key, value in pairs(branch_selector_control.wait_branch_info_games) do
            if tostring(value) == tostring(UserData) then
                pos = key;
            end
        end
        
        if pos ~= nil then
            table.remove(branch_selector_control.wait_branch_info_games, pos);
            local tpfui = ierd_tgp.tpf_ui.GetTPFUI();
            if nil ~= tpfui then
                tpfui:RemoveInterval("branch_selector_control.on_wait_branch_info_timeout", tonumber(UserData));
            end;
            ierd_tgp.tpf_ui.Require("branch_selector\\view.lua");        
            branch_selector_view.open_branch_selector({game_id = UserData});
        end
    end;
};
branch_selector_control.init();
